package com.moarea.app.service.abst;

import com.moarea.app.dao.SystemLogMapper;
import com.moarea.app.file.imp.FileHandlerFactory;
import com.moarea.app.file.inter.FileHandler;
import com.moarea.app.lang.utils.StringUtils;
import com.moarea.app.model.LoginUser;
import com.moarea.app.model.SystemLog;
import com.moarea.app.resource.ResourceRepository;
import com.moarea.app.service.inter.IBillBaseService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.Map;

/**
 * 业务公共处理
 * <p>
 * jizhong
 * <p>
 * 2017-09-20
 */
public abstract class ABillBaseService extends AFileService implements IBillBaseService {

    //系统基础参数
    protected static final String APPNO = properties.getProperty("upms.appno");
    protected static final String APPNAME = properties.getProperty("upms.appname");
    protected static final String UPMS_URL = properties.getProperty("upms.url");

    protected static FileHandler fileHandler = FileHandlerFactory.newFileHandler();

    /**
     * 取出Map里面所有的value前后空格
     *
     * @param params 待处理Map集合
     */
    @Override
    public void MapValueTrim(Map<String, Object> params) {
        if (params != null) {
            Object[] keys = params.keySet().toArray();
            for (int i = 0; i < keys.length; i++) {
                String key_value = params.get(keys[i].toString()) == null ? "" : params.get(keys[i].toString()).toString().trim();
                params.put(keys[i].toString(), key_value);
            }
        }
    }

    /**
     * 获取IP
     *
     * @param request HttpServletRequest
     * @return IP地址
     */
    @Override
    public String getIpAddr(HttpServletRequest request) {

        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }

    /**
     * 保存日志
     *
     * @param MODULE  模块
     * @param CONTENT 内容
     */
    @Override
    public void saveLog(String MODULE, String CONTENT, SystemLogMapper systemLogMapper,LoginUser loginUser) {

        SystemLog systemLog = new SystemLog();
        systemLog.setCONTENT(CONTENT);
        systemLog.setIP(loginUser.getIP_ADDR());
        systemLog.setMODULE(MODULE);
        systemLog.setOPERATE_TIME(new Date());
        systemLog.setOPERATE_USER(loginUser.getUSERNAME());
        systemLog.setSYSTEM(loginUser.getAPPNO());

        systemLogMapper.insert(systemLog);

    }

    /**
     * 根据前端下拉框的动态输入
     * <p>
     * 对字段进行拼接
     *
     * @param param 查询参数
     */
    protected void getSql(Map<String, Object> param) {
        if (StringUtils.isNotBlank(param, "keyField")) {
            if (StringUtils.isNotBlank(param, "inputValue")) {
                param.put("sql", "and (".concat(param.get("idField").toString()).concat(" like")
                        .concat(" '%").concat(param.get("inputValue").toString()).concat("%' ").concat("or ")
                        .concat(param.get("keyField").toString()).concat(" like")
                        .concat(" '%").concat(param.get("inputValue").toString()).concat("%')"));
            }
            param.put("limit", "limit 10");
        }
    }


}
